package com.yic.module.device.controller.admin.refund;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.device.dal.dataobject.device.DeviceDO;
import com.yic.module.device.service.device.DeviceService;
import com.yic.module.system.api.user.AdminUserApi;
import com.yic.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.device.controller.admin.refund.vo.*;
import com.yic.module.device.dal.dataobject.refund.RefundDO;
import com.yic.module.device.convert.refund.RefundConvert;
import com.yic.module.device.service.refund.RefundService;

@Tag(name = "管理后台 - 退还申请")
@RestController
@RequestMapping("/device/refund")
@Validated
public class RefundController {

    @Resource
    private RefundService refundService;
    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private DeviceService deviceService;

    @PostMapping("/create")
    @Operation(summary = "创建退还申请")
    @PreAuthorize("@ss.hasPermission('device:refund:create')")
    public CommonResult<Long> createRefund(@Valid @RequestBody RefundCreateReqVO createReqVO) {
        return success(refundService.createRefund(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新退还申请")
    @PreAuthorize("@ss.hasPermission('device:refund:update')")
    public CommonResult<Boolean> updateRefund(@Valid @RequestBody RefundUpdateReqVO updateReqVO) {
        refundService.updateRefund(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除退还申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('device:refund:delete')")
    public CommonResult<Boolean> deleteRefund(@RequestParam("id") Long id) {
        refundService.deleteRefund(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得退还申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:refund:query')")
    public CommonResult<RefundRespVO> getRefund(@RequestParam("id") Long id) {
        RefundDO refund = refundService.getRefund(id);
        return success(RefundConvert.INSTANCE.convert(refund));
    }

    @GetMapping("/list")
    @Operation(summary = "获得退还申请列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('device:refund:query')")
    public CommonResult<List<RefundRespVO>> getRefundList(@RequestParam("ids") Collection<Long> ids) {
        List<RefundDO> list = refundService.getRefundList(ids);
        return success(RefundConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得退还申请分页")
    @PreAuthorize("@ss.hasPermission('device:refund:query')")
    public CommonResult<PageResult<RefundRespVO>> getRefundPage(@Valid RefundPageReqVO pageVO) {
        PageResult<RefundDO> pageResult = refundService.getRefundPage(pageVO);
        PageResult<RefundRespVO> resp = RefundConvert.INSTANCE.convertPage(pageResult);
        resp.getList().forEach(item -> {
            // 封装用户
            AdminUserRespDTO user = adminUserApi.getUser(item.getUserId());
            if (ObjectUtil.isNotEmpty(user)) {
                item.setUserName(user.getNickname());
            }
            // 封装设备
            DeviceDO device = deviceService.get(item.getDeviceId());
            if (ObjectUtil.isNotEmpty(device)) {
                item.setDeviceName(device.getName());
                item.setDeviceCode(device.getCode());
            }
        });
        return success(resp);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出退还申请 Excel")
    @PreAuthorize("@ss.hasPermission('device:refund:export')")
    @OperateLog(type = EXPORT)
    public void exportRefundExcel(@Valid RefundExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<RefundDO> list = refundService.getRefundList(exportReqVO);
        // 导出 Excel
        List<RefundExcelVO> datas = RefundConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "退还申请.xls", "数据", RefundExcelVO.class, datas);
    }

    @PutMapping("/approve")
    @Operation(summary = "审核接口")
    @PreAuthorize("@ss.hasPermission('device:refund:approve')")
    public CommonResult<Boolean> approveRefund(@RequestParam("id") Long id){
        refundService.approveRefund(id);
        return success(true);
    }

    @PutMapping("/cancel")
    @Operation(summary = "撤审接口")
    @PreAuthorize("@ss.hasPermission('device:refund:approve')")
    public CommonResult<Boolean> cancelRefund(@RequestParam("id") Long id){
        refundService.cancelRefund(id);
        return success(true);
    }

}
